package com.bng8.superstar_cms.util;

import org.apache.commons.lang3.RandomStringUtils;

import java.security.MessageDigest;

/**
 * ClassName: ShaUtils
 * Package: com.bng8.superstar_cms.util
 * Date: 2023/8/17 9:29
 * Author: 安知（13921877634）
 */
public class ShaUtils {
    private static final String SHA_256_ALGORITHM = "SHA-256";

    public static String encrypt(String data) throws Exception {
        //获取SHA-256算法实例
        MessageDigest messageDigest = MessageDigest.getInstance(SHA_256_ALGORITHM);
        //计算散列值
        byte[] digest = messageDigest.digest(data.getBytes());
        StringBuilder stringBuilder = new StringBuilder();
        //将byte数组转换为15进制字符串
        for (byte b : digest) {
            stringBuilder.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 2);
        }
        return stringBuilder.toString();
    }

    /**
     * 生产盐值
     *
     * @return
     */
    public static String getSalt() {
        return RandomStringUtils.randomAlphabetic(32);
    }
}
